#include "filedrawwindow.h"
#include "ui_fileDrawWindow.h"

//鼠标事件
#include<QMouseEvent>
#include<QApplication>
//文件对话框
#include<QFileDialog>
#include<QDebug>


FileDrawWindow::FileDrawWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::FileDrawWindow)
{
    ui->setupUi(this);
    openglWidget=ui->openGLWidget;//获取绘图窗口
    //搜索框
    search=new QLineEdit();
    ui->toolBar->addWidget(search);
    search->setPlaceholderText("在这里进行搜索");

    //连接信号和槽
    connect(ui->actionMove,SIGNAL(triggered()),this,SLOT(onActionMovePressed()),Qt::AutoConnection);
    connect(ui->actionScale,SIGNAL(triggered()),this,SLOT(onActionScalePressed()),Qt::AutoConnection);
    connect(ui->actionReset,SIGNAL(triggered()),this,SLOT(onActionResetPressed()),Qt::AutoConnection);
    connect(ui->actionTable,SIGNAL(triggered()),this,SLOT(onActionTablePressed()),Qt::AutoConnection);
    connect(openglWidget,SIGNAL(mapLoaded(geoMap*)),this,SLOT(onMapLoaded()),Qt::AutoConnection);
    connect(openglWidget,SIGNAL(mapLoaded(geoMap*)),&catalog,SLOT(initCatalog(geoMap*)),Qt::AutoConnection);
    connect(ui->actionColor,SIGNAL(triggered()),this,SLOT(onActionColorPressed()),Qt::AutoConnection);
    connect(search,SIGNAL(editingFinished()),this,SLOT(onSearch()),Qt::AutoConnection);
    connect(this,SIGNAL(searchStateChanged(int,int)),&catalog,SLOT(onSearchStateChanged(int,int)),Qt::AutoConnection);
    connect(ui->actionSelect,SIGNAL(triggered()),this,SLOT(onActionSelectPressed()),Qt::AutoConnection);
}

FileDrawWindow::~FileDrawWindow()
{
    delete ui;
}

void FileDrawWindow::onActionMovePressed()
{
    if(openglWidget->mouseStatus!=mouseStat::MOVE)
    {
        openglWidget->mouseStatus=MOVE;
        ui->actionScale->setChecked(false);
        openglWidget->setCursor(Qt::OpenHandCursor);//鼠标样式
    }
    else
    {
        openglWidget->mouseStatus=mouseStat::NONE;
        openglWidget->setCursor(Qt::ArrowCursor);
    }
}

void FileDrawWindow::onActionScalePressed()
{
    if(openglWidget->mouseStatus!=mouseStat::SCALE)
    {
        ui->actionMove->setChecked(false);
        openglWidget->setCursor(Qt::ArrowCursor);
        openglWidget->mouseStatus=SCALE;
    }
    else
    {
        openglWidget->mouseStatus=mouseStat::NONE;
    }
}

void FileDrawWindow::onActionSelectPressed()
{
    if(openglWidget->isIndexSelected==false)
    {
        QMessageBox msg;
        msg.setText("未选择当前操作索引。");
        msg.setWindowTitle("警告");
        msg.exec();
        disconnect(ui->actionSelect,SIGNAL(triggered()),this,SLOT(onActionSelectPressed()));
        ui->actionSelect->trigger();
        connect(ui->actionSelect,SIGNAL(triggered()),this,SLOT(onActionSelectPressed()),Qt::AutoConnection);
        return;

    }
    if(openglWidget->mouseStatus!=mouseStat::SELECT)
    {
        openglWidget->mouseStatus=SELECT;
        ui->actionScale->setChecked(false);
    }
    else
    {
        openglWidget->mouseStatus=mouseStat::NONE;
        foreach(geoLayer*layer,openglWidget->map.geoLayers)
        {
            foreach(geoObject*object,layer->geoObjects)
            {
                if(object->isSelected==true)
                {
                    object->isSelected=false;
                }
            }
        }
        openglWidget->update();
    }

}

void FileDrawWindow::onActionResetPressed()
{
    if(openglWidget->mouseStatus!=mouseStat::NONE)
    {
        ui->actionMove->setChecked(false);
        ui->actionScale->setChecked(false);
        openglWidget->mouseStatus=NONE;
        openglWidget->resetMap();
        openglWidget->setCursor(Qt::ArrowCursor);
    }
}

void FileDrawWindow::onActionTablePressed()
{
    if(catalog.isVisible()==false)
    {
        catalog.show();
    }
    else
    {
        catalog.hide();
    }
}

void FileDrawWindow::onMapLoaded()
{
    //调整窗口大小
    windowWidth=openglWidget->getWidth();
    windowHeight=openglWidget->getHeight();
    int mul;//缩放比例
    //使size不要超过限制
    if(windowWidth>1000000)   mul=8000;
    else if(windowWidth>100000) mul=1000;
    else    mul=100;
    resize((static_cast<int>(windowWidth))/mul,(static_cast<int>(windowHeight))/mul);
}

void FileDrawWindow::onActionColorPressed()
{
    QString filePath;
    //从FileDialog获取文件路径
    filePath=QFileDialog::getOpenFileName(nullptr,QFileDialog::tr("打开文件"),"",QFileDialog::tr("sld文件 (*.sld)"));
    openglWidget->readSLD(filePath);//读取sld文件
}

void FileDrawWindow::onSearch()
{
    QString query=search->text();//获取搜索请求
    geoMap *map=&openglWidget->map;//获取地图
    foreach(geoLayer*layer,map->geoLayers)
    {
        foreach(geoObject*object,layer->geoObjects)
        {
            if(object->isSearched==true)    object->isSearched=false;
        }
    }
    if(query!=nullptr&&map!=nullptr)
    {
        for(int i=0;i<map->geoLayers.size();i++)
        {
            geoLayer* layer=map->geoLayers[i];
            if(layer->name.contains(query))//若图层名匹配，则下属所有object均被搜索到
            {
                for(int j=0;j<layer->geoObjects.size();j++)
                {
                    layer->geoObjects[j]->isSearched=true;
                    searchStateChanged(i,j);//发出信号
                }
            }
            else
            {
                for(int j=0;j<layer->geoObjects.size();j++)//反之对每个object进行匹配
                {
                    if(layer->geoObjects[j]->name.contains(query))
                    {
                        layer->geoObjects[j]->isSearched=true;
                        searchStateChanged(i,j);//发出信号
                    }
                }
            }
        }
        openglWidget->update();
    }
    else return;
}

